package com.itqf.controller;

import com.alibaba.fastjson.JSON;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/9
 * @Time: 下午3:47
 */
@Controller  //控制层
@RequestMapping("/first")  //根路径  localhost:8080/first/xxxx  可选
public class FirstController {



    //localhost:8080/first/hello
    @RequestMapping("/hello")//用户输入的请求路径映射
    public  String myFirst(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request+"--"+response);

        System.out.println(request.getParameter("name"));

        request.setAttribute("name","我的springmvc的第二个案例");

       // return "/WEB-INF/jsp/first.jsp";//物理视图
        return  "first";//逻辑视图
        ///"WEB-INF/jsp/"+ "first"+"jsp" = 物理视图
    }


    //localhost:8080/first/getParam1?id=1&username=张三
    //一般请求参数的key和方法的参数的变量名一致
    @RequestMapping("/getParam1")
    //@RequestParam("username")  String  name  限制key为username   并且把该key对应的值封装到name参数中
    public   String param1(int id,@RequestParam("username") String name){//key 与参数名对应
        System.out.println("id="+id+"--username="+name);
        return  "first";
    }

    @RequestMapping("/getParam2")
    //User的属性需要和请求携带的参数的key对应
    public   String param2(User user){//key 与参数名对应
        System.out.println(user);
        return  "first";
    }

    // //默认get请求
    @RequestMapping(value = "/user/{uid}",method = RequestMethod.GET)//  配置：/user/数值
    //localhost:8080/first/user/10
    //@PathVariable("uid")  为了获得url中{uid} 位置的值
    public  void  param3(@PathVariable("uid") int  uid,HttpServletResponse response){

        System.out.println(uid+"获取  编号为"+uid+"的用户信息");

        Map map = new HashMap();
        User user = new User();//数据库查询的
        user.setId(1);
        user.setName("张三");
        map.put("user",user);

        String  json = JSON.toJSONString(map);
        try {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(json);

        }catch (Exception e){}

    }
    // //默认get请求
    @RequestMapping(value = "/user/{uid}",method = RequestMethod.DELETE)//  配置：/user/数值
    //localhost:8080/first/user/10
    //@PathVariable("uid")  为了获得url中{uid} 位置的值
    public  void  param3Delete(@PathVariable("uid") int  uid,HttpServletResponse response){

        System.out.println(uid+"删除  编号为"+uid+"的用户信息");
        Map map = new HashMap();
        map.put("code",1);
        map.put("msg","删除成功");
        String  json = JSON.toJSONString(map);
        try {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(json);

        }catch (Exception e){}

    }


    //默认get请求
    @RequestMapping(value = "/user/{uid}/{uname}")//  配置：/user/数值
    //localhost:8080/first/user/10/张三
    //@PathVariable("uid")  为了获得url中{uid} 位置的值
    //删除  delete?id=1
    //rest  user/1  (delete请求)
    //rest  user/1  (get请求)

    public  void  param4(@PathVariable("uid") int  uid,@PathVariable("uname") String name){

        System.out.println(uid+"----"+name);

    }




}
